<?php

namespace App\Admin\Controllers\Stock;

use App\Admin\Actions\Indepot;
use App\Admin\Actions\Outdepot;
use App\Models\ChinaArea;
use App\Models\StockDepot;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class StockDepotController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('仓库管理')
            ->description('仓库管理')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.detail'))
            ->description(trans('仓库'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.edit'))
            ->description(trans('仓库'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.create'))
            ->description(trans('仓库'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new StockDepot);
        $grid->model()->orderByDesc('updated_at');
        if (Admin::user()->depot_id) {
            $grid->model()->where("id", Admin::user()->depot_id);
        }
        $grid->id('ID');
        $grid->name('仓库名称');
        $grid->contact('联系人');
        $grid->phone('电话');
        $grid->province('省')->display(function ($released){
            $return = ChinaArea::where('code', $released)->first();
            return $return['name'];
        });
        $grid->city('市')->display(function ($released){
            $return = ChinaArea::where('code', $released)->first();
            return $return['name'];
        });
        $grid->district('区')->display(function ($released){
            $return = ChinaArea::where('code', $released)->first();
            return $return['name'];
        });
        $grid->address('详细地址');
//        $grid->status('状态')->using(['是' , '否']);
        $grid->created_at(trans('admin.created_at'));
        $grid->updated_at(trans('admin.updated_at'));

        $grid->actions(function ($actions) {
            $actions->add(new Indepot());
            $actions->add(new Outdepot());
        });

        $grid->quickSearch('name', 'contact', 'phone');
        $grid->filter(function($filter){
            $filter->disableIdFilter();// 去掉默认的id过滤器
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(StockDepot::findOrFail($id));

        $show->id('ID');
        $show->name('仓库名称');
        $show->contact('联系人');
        $show->phone('电话');
        $show->province('省份');
        $show->city('城市');
        $show->district('区域');
        $show->address('详细地址');
        $show->status('状态');
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new StockDepot);

        $form->text('name', '仓库名称')->rules('required|min:2');
        $form->text('contact', '联系人')->rules('required|min:2');
        $form->text('phone', '电话')->rules('required|min:8');
        $form->distpicker(['province', 'city', 'district'], '地域选择')
//            ->default([
//                'province' => 130000,
//                'city'     => 130200,
//                'district' => 130201,
//            ])
            ->rules('required');
//        $form->text('province', '省份')->rules('required');
//        $form->text('city', '城市')->rules('required');
//        $form->text('district', '区域')->rules('required');
        $form->text('address', '详细地址')->rules('required');
        $form->radio('status', '状态')->options([0=>'禁用', '1'=>"启用"])->default(0);
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }
}
